extends Node
class_name InteractionHandler

var interactable
@onready var interaction_ui: InteractionUI = $"../InteractionUI"
@onready var inventory_ui: InventoryUI = $"../InventoryUI"
@onready var animation_tree: AnimationTree = $"../AnimationTree"

func toggle_interaction(is_enabled: bool, interactable):
	if is_enabled:
		interaction_ui.set_item_label_text("Press I To Interact")
	else:
		interaction_ui.hide()

	if interactable && interactable.has_signal("picked_up") && !interactable.picked_up.is_connected(on_item_pickup):
		interactable.picked_up.connect(on_item_pickup)
	elif self.interactable && self.interactable.has_signal("picked_up") && self.interactable.picked_up.is_connected(on_item_pickup):
		self.interactable.picked_up.disconnect(on_item_pickup)
	self.interactable = interactable

func on_item_pickup(item: Item) -> void:
	inventory_ui.add_item(item)

func clean_interactable():
	if interactable:
		interactable.queue_free()

func _input(event: InputEvent) -> void:
	if Input.is_action_just_pressed("interact") && interactable && interactable.has_method("interact"):
		interactable.interact()
		animation_tree.set("parameters/OneShot/request", AnimationNodeOneShot.ONE_SHOT_REQUEST_FIRE)
